<template>
  <div class="department-select-bg">
    <van-nav-bar title="选择科室" left-arrow @click-left="goBack">
      <template #right>
        <van-icon name="ellipsis" size="22" />
      </template>
    </van-nav-bar>
    <div class="campus-tabs">
      <van-button
        v-for="campus in campusList"
        :key="campus"
        :type="selectedCampus === campus ? 'primary' : 'default'"
        size="small"
        @click="selectCampus(campus)"
        class="campus-btn"
      >
        {{ campus }}
      </van-button>
    </div>
    <div class="department-main">
      <div class="department-left">
        <div
          v-for="dept in firstLevelDepartments"
          :key="dept.id"
          :class="['department-item', { active: selectedFirstLevel && selectedFirstLevel.id === dept.id }]"
          @click="selectFirstLevel(dept)"
        >
          {{ dept.name }}
        </div>
      </div>
      <div class="department-right">
        <div
          v-for="dept in secondLevelDepartments"
          :key="dept.id"
          class="department-item"
          @click="selectSecondLevel(dept)"
        >
          {{ dept.name }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const allDepartments = ref([])
const campusList = ref([])
const selectedCampus = ref('')
const selectedFirstLevel = ref(null)

const firstLevelDepartments = computed(() => {
  return allDepartments.value.filter(
    d => !d.parent && d.campus === selectedCampus.value
  )
})
const secondLevelDepartments = computed(() => {
  if (!selectedFirstLevel.value) return []
  return allDepartments.value.filter(
    d => d.parent === selectedFirstLevel.value.id && d.campus === selectedCampus.value
  )
})

function goBack() {
  router.back()
}

function selectCampus(campus) {
  selectedCampus.value = campus
  // 切换院区时，自动选中第一个一级科室
  const first = allDepartments.value.find(d => d.parent === null && d.campus === campus)
  selectedFirstLevel.value = first || null
}

function selectFirstLevel(dept) {
  selectedFirstLevel.value = dept
}

function selectSecondLevel(dept) {
  // 这里可以跳转到下一个页面，或 emit 事件
  // router.push({ path: '/booking2', query: { departmentId: dept.id } })
}

async function fetchDepartments() {
  const res = await axios.get('http://localhost:8000/background/departments/')
  allDepartments.value = res.data || []
  // 获取所有院区
  const campusSet = new Set(allDepartments.value.map(d => d.campus))
  campusList.value = Array.from(campusSet)
  // 默认选中第一个院区和第一个一级科室
  selectedCampus.value = campusList.value[0] || ''
  const first = allDepartments.value.find(d => d.parent === null && d.campus === selectedCampus.value)
  selectedFirstLevel.value = first || null
}

onMounted(() => {
  fetchDepartments()
})
</script>

<style scoped>
.department-select-bg {
  min-height: 100vh;
  background: #f6f7fb;
}
.campus-tabs {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 16px 18px 0 18px;
  gap: 12px;
}
.campus-btn {
  margin-right: 8px;
}
.department-main {
  display: flex;
  height: 600px;
  background: #fff;
  border-radius: 12px;
  margin: 18px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  overflow: hidden;
}
.department-left {
  width: 100px;
  background: #fafbfb;
  border-right: 1px solid #f2f2f2;
  display: flex;
  flex-direction: column;
}
.department-item {
  padding: 18px 12px;
  font-size: 16px;
  color: #333;
  cursor: pointer;
  text-align: left;
  background: transparent;
  border: none;
  outline: none;
  transition: background 0.2s, color 0.2s;
}
.department-item.active {
  color: #338bff;
  background: #f6f7fb;
  font-weight: bold;
}
.department-right {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #fff;
}
</style>
